package zld369504.tw.m_team.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;

import zld369504.tw.m_team.R;

public class GroupLayout extends LinearLayout {

    private Paint paint;
    private Paint textPaint;
    private int strokeWidth = 1;

    private int startY = 0;
    private int startX = 0;
    private int stopX = 0;
    private int stopY = 0;

    float leftPadding;
    float topPadding;
    float rightPadding;
    float bottomPadding;


    private String titleText;
    private int titleTextColor;
    private int titleTextSize;
    private int borderColor;

    public static class Params {
        public String titleText=" ";
        public int titleTextColor=Color.BLACK;
        public int titleTextSize=0;
        public int borderColor=Color.BLACK;
    }

    public GroupLayout(Context context) {
        super(context);
    }

    public GroupLayout(Context context,Params params) {
        super(context);
        setGroupLayoutAttr(params);
    }

    public GroupLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        getSetAttr(context, attrs);
        setupPaint();
    }

    public GroupLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getSetAttr(context, attrs);
        setupPaint();
    }

    public void setGroupLayoutAttr(Params params){
        titleText=params.titleText;
        titleTextColor=params.titleTextColor;
        if(params.titleTextSize==0){
            titleTextSize= (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_SP,
                    16,
                    getResources().getDisplayMetrics());
        } else {
            titleTextSize=params.titleTextSize;
        }
        borderColor=params.borderColor;
        setupPaint();
    }

    private void getSetAttr(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GroupLayout);
        titleText = array.getString(R.styleable.GroupLayout_titleText);
        if (TextUtils.isEmpty(titleText)) {
            titleText = " ";
        }
        titleTextColor = array.getColor(R.styleable.GroupLayout_titleTextColor, Color.BLACK);
        titleTextSize = (int) array.getDimension(
                R.styleable.GroupLayout_titleTextSize,
                TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_SP,
                        16,
                        getResources().getDisplayMetrics()));
        borderColor = array.getColor(R.styleable.GroupLayout_borderColor, Color.BLACK);
        array.recycle();
    }

    private void setupPaint() {
        paint = new Paint();
        paint.setColor(borderColor);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(strokeWidth);
        paint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint();
        textPaint.setColor(titleTextColor);
        textPaint.setTextSize(titleTextSize);
        textPaint.setAntiAlias(true);
        startY = (int) ((textPaint.descent() - textPaint.ascent()) / 2);

        topPadding = textPaint.descent() - textPaint.ascent();

        leftPadding = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                8,
                getResources().getDisplayMetrics());

        rightPadding = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                0,
                getResources().getDisplayMetrics());
        bottomPadding = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                8,
                getResources().getDisplayMetrics());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        stopX = width - strokeWidth;
        stopY = height - strokeWidth;

        //draw left line
        canvas.drawLine(startX, startY, startX, stopY, paint);
        //draw bottom line
        canvas.drawLine(startX, stopY, stopX, stopY, paint);
        //draw right line
        canvas.drawLine(stopX, startY, stopX, stopY, paint);

        //draw first top line
        canvas.drawLine(startX, startY, 30, startY, paint);

        //draw text
        canvas.drawText(titleText, startX + 40, titleTextSize, textPaint);
        float textWidth = textPaint.measureText(titleText);

        //draw second top line
        canvas.drawLine(startX + 40 + textWidth + 10, startY, stopX, startY, paint);
    }
}
